package com.neuedu.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.neuedu.entity.UmsStudent;

import java.util.Date;

public class JwtUtil {
    public static final String SECRET = "wangyu";
    public static String create(SystemUser systemUser) {
        return JWT.create()
                .withClaim("id", systemUser.getId())
                .withClaim("src", systemUser.getSrc())
                .withClaim("role", systemUser.getRole())
                .withClaim("time", new Date())
                .sign(Algorithm.HMAC256(SECRET));
    }
    public static SystemUser decode(String token) {
        DecodedJWT verify = JWT.require(Algorithm.HMAC256(SECRET))
                .build()
                .verify(token);
        SystemUser systemUser = new SystemUser();
        systemUser.setId(verify.getClaim("id").asLong());
        systemUser.setSrc(verify.getClaim("src").asString());
        systemUser.setRole(verify.getClaim("role").asString());
        return systemUser;
    }
}
